package gui;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;

import client.ClientConsole;

import logic.LateInReturnReport;
import logic.Message;

/** 
 * produces and shows the late in return report
 * shows statistic information for late in return books
 */
public class LateInReturnReportWindow extends JPanel implements ActionListener{
	/**  GUI Panels used for Design*/
	private JPanel mainPanel,reportPanel,backbottom,topPanel;
	/**error label show the feedback from server*/
	private JLabel avgLabel,medianLabel,numOfDelaysLabel,average,median,numOfDelays,
	dateLabel,error;
	private JButton back;
	/** saves the properties of the GridBagLayout for*/
	GridBagConstraints gbc = new GridBagConstraints();
	/** holds statistic information for each book*/
	LateInReturnReport [] lateBooks;
	
	/**Default constructor-Generates and create the late in return reports*/
	public LateInReturnReportWindow()
	{
		super();
		int index=1;
		int i=5;
		setSize(300, 300);
		lateBooks=null;
		setLayout(new BorderLayout());
		

			mainPanel=new JPanel();
			mainPanel.setLayout(new BorderLayout());
			topPanel=new JPanel();
			topPanel.setLayout(new BorderLayout());
			dateLabel=new JLabel();
			topPanel.add(dateLabel,BorderLayout.WEST);
			mainPanel.add(topPanel,BorderLayout.NORTH);
			reportPanel=new JPanel();
			reportPanel.setLayout(new GridBagLayout());
			gbc.insets= new Insets(7,7,7,7);
			avgLabel=new JLabel("Total Average: ");
			gbc = new GridBagConstraints();
			gbc.gridx=0;
			gbc.gridy=0;
			gbc.insets= new Insets(4,4,4,4);
			reportPanel.add(avgLabel,gbc);
			average=new JLabel();
			gbc = new GridBagConstraints();
			gbc.gridx=1;
			gbc.gridy=0;
			gbc.insets= new Insets(4,4,4,4);
			reportPanel.add(average,gbc);
			medianLabel=new JLabel("Median: ");
			gbc = new GridBagConstraints();
			gbc.gridx=0;
			gbc.gridy=1;
			gbc.insets= new Insets(4,4,4,4);
			reportPanel.add(medianLabel,gbc);
			median=new JLabel();
			gbc = new GridBagConstraints();
			gbc.gridx=1;
			gbc.gridy=1;
			gbc.insets= new Insets(4,4,4,4);
			reportPanel.add(median,gbc);
			numOfDelaysLabel=new JLabel("number of delays: ");
			gbc = new GridBagConstraints();
			gbc.gridx=0;
			gbc.gridy=2;
			gbc.insets= new Insets(4,4,4,4);
			reportPanel.add(numOfDelaysLabel,gbc);
			numOfDelays=new JLabel();
			gbc = new GridBagConstraints();
			gbc.gridx=1;
			gbc.gridy=2;
			gbc.insets= new Insets(4,4,4,4);
			reportPanel.add(numOfDelays,gbc);
			
			JLabel bookID=new JLabel("Book ID");
			gbc = new GridBagConstraints();
			gbc.gridx=0;
			gbc.gridy=4;
			gbc.insets= new Insets(4,4,4,4);
			reportPanel.add(bookID,gbc);
			JLabel avg=new JLabel("Average");
			gbc = new GridBagConstraints();
			gbc.gridx=1;
			gbc.gridy=4;
			gbc.insets= new Insets(4,4,4,4);
			reportPanel.add(avg,gbc);
			JLabel med=new JLabel("Median");
			gbc = new GridBagConstraints();
			gbc.gridx=2;
			gbc.gridy=4;
			gbc.insets= new Insets(4,4,4,4);
			reportPanel.add(med,gbc);
			JLabel delays=new JLabel("# of delays");
			gbc = new GridBagConstraints();
			gbc.gridx=3;
			gbc.gridy=4;
			gbc.insets= new Insets(4,4,4,4);
			reportPanel.add(delays,gbc);
			i=5;
			produceReport();
			if (lateBooks!=null)
			{
			//show all books
			for (index=1;index<lateBooks.length;index++)
			{
				bookID=new JLabel(String.valueOf(lateBooks[index].getBookCatalog()));
				gbc = new GridBagConstraints();
				gbc.gridx=0;
				gbc.gridy=i;
				gbc.insets= new Insets(4,4,4,4);
				reportPanel.add(bookID,gbc);
				avg=new JLabel(String.valueOf(lateBooks[index].getAvg()));
				gbc = new GridBagConstraints();
				gbc.gridx=1;
				gbc.gridy=i;
				gbc.insets= new Insets(4,4,4,4);
				reportPanel.add(avg,gbc);
				med=new JLabel(String.valueOf(lateBooks[index].getMedian()));
				gbc = new GridBagConstraints();
				gbc.gridx=2;
				gbc.gridy=i;
				gbc.insets= new Insets(4,4,4,4);
				reportPanel.add(med,gbc);
				delays=new JLabel(String.valueOf(lateBooks[index].getNumberOfDelays()));
				gbc = new GridBagConstraints();
				gbc.gridx=3;
				gbc.gridy=i++;
				gbc.insets= new Insets(4,4,4,4);
				reportPanel.add(delays,gbc);
			}
			mainPanel.add(reportPanel,BorderLayout.CENTER);
		}
		
		back=new JButton("back");
		backbottom=new JPanel();
		backbottom.add(back);
		error=new JLabel();
		error.setVisible(false);
		backbottom.add(error);
		back.addActionListener(this);
		mainPanel.add(backbottom,BorderLayout.SOUTH);
		add(mainPanel,BorderLayout.CENTER);
		
	}
	/**
	 * sends a request to generate new Report and show its properties
	 */
	private void produceReport() {
		
		Message msg=new Message ("Get Late In Return Report",null);
		ClientConsole.HandleMessageFromGui(msg);
		msg=(Message)ClientConsole.client.getMessage();
		if (msg.getCommand().equals("Returned Lend Report"))
		{
			if (((LateInReturnReport []) msg.getObjects()).length>0)
			{
				lateBooks=(LateInReturnReport []) msg.getObjects();
				System.out.println(lateBooks[0].getMedian());
				System.out.println(lateBooks[0].getAvg());
				average.setText(String.valueOf(lateBooks[0].getAvg()));
				median.setText(String.valueOf(lateBooks[0].getMedian()));
				numOfDelays.setText(String.valueOf(lateBooks[0].getNumberOfDelays()));
				dateLabel.setText(lateBooks[0].getReportDate().toString());
			}
			else
			{
				error.setVisible(true);
				error.setText("Not enough Data");
			}
				
		}
		else
		{
			error.setText(msg.getCommand());
		}
	}
	//Handles user's clicks on buttons
	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource()==back)
		{
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(new ProduceReportWindow());
		}
		
	}
}
